using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeYiHome.PublicLibrary.Extension;

namespace GeYiHome.PublicLibrary
{
    public class OpenApiException<TExpType> : Exception
    {
        private readonly string _exceptionMsg;
        private readonly object[] _args;
        private readonly TExpType _expType;

        public OpenApiException(TExpType expType)
        {
            _expType = expType;
        }

        public OpenApiException(TExpType expType, params object[] args)
        {
            _expType = expType;
            _args = args;
        }

        public OpenApiException(string exceptionMsg, params object[] args)
        {
            _exceptionMsg = exceptionMsg;
            _args = args;
        }

        public override string Message
        {
            get
            {
                var param = _args ?? new object[0];
                var msg = string.IsNullOrWhiteSpace(_exceptionMsg)
                    ? string.Format(_expType.Description() + " (ErrorCode:" + GetErrorCode() + ")", param)
                    : string.Format(_exceptionMsg, param);
                return msg;
            }
        }

        private int GetErrorCode()
        {
            return Convert.ToInt32(Enum.Parse(typeof(TExpType), _expType.ToString()));
        }
    }
}
